package cn.xh.org.controller;

import cn.xh.basic.util.AjaxResult;
import cn.xh.basic.util.BusinessException;
import cn.xh.basic.util.PageList;
import cn.xh.org.domain.Employee;
import cn.xh.org.domain.Shop;
import cn.xh.org.query.ShopQuery;
import cn.xh.org.service.IEmployeeService;
import cn.xh.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺管理接口
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @Autowired
    private IEmployeeService employeeService;

     //新增和修改  put
    @PutMapping
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() != null){
                shopService.update(shop);
            }else{
                shopService.add(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage(e.getMessage());
        }
    }

    /**
     * PathVariable  从URL中获取占位符的值
     * @param id
     */
    @DeleteMapping("/{id}")   //  /shop/1
    public AjaxResult delete(@PathVariable("id") Long id){
        //链式编程思维
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage(e.getMessage());
        }


    }


    //查询一条  get
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }

    //查询所有  get
    @GetMapping("/list")
    public List<Shop> getAll(){
        return shopService.queryAll();
    }

    //分页+高级查询 post
    @PostMapping
    public PageList<Shop> getPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    /**
     * 如果前端传的是，数组那么后端可以用  list 或者array接收都可以
     * @param ids 需要批量删除的id
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage(e.getMessage());
        }

    }





}
